/*=====================================================
Critical Links
Auto Configuration Server

Authors:
	Rui Eduardo Gouveia Gil - r-gil@critical-links.com

Date: 2010
Version 0.1
=======================================================*/
#ifndef __DISKFILESENDER_H__
#define __DISKFILESENDER_H__

#include <pion/PionHashMap.hpp>
#include <pion/PionException.hpp>
#include <pion/net/WebService.hpp>
#include <pion/net/HTTPServer.hpp>
#include <pion/net/HTTPRequest.hpp>
#include <rgcpp/utils/LogHandler.h>
#include <pion/net/HTTPResponseWriter.hpp>

#include "httpd/DiskFile.h"

namespace acs
{

class DiskFileSender : public boost::enable_shared_from_this<DiskFileSender>, private boost::noncopyable
{
	private:
		DiskFile diskFile;
		unsigned long bytesSent;
		unsigned long maxChunkSize;
		unsigned long fileBytesToSend;
		boost::shared_array<char> contentBuf;
		boost::filesystem::ifstream fileStream;
		pion::net::HTTPResponseWriterPtr writer;

	public:
		static boost::shared_ptr<DiskFileSender> create(DiskFile& file, pion::net::HTTPRequestPtr& request, pion::net::TCPConnectionPtr& tcpConnection, unsigned long maxChunkSize = 0);

		virtual ~DiskFileSender();

		void send(void);

	protected:
		DiskFileSender(DiskFile& file, pion::net::HTTPRequestPtr& request, pion::net::TCPConnectionPtr& tcpConn, unsigned long maxSize);

		void handleWrite(const boost::system::error_code& write_error, std::size_t bytes_written);
};

}

#endif
